package com.hxf.dao.preferential.impl;

import java.util.ArrayList;
import java.util.List;

import oracle.net.aso.p;

import org.springframework.stereotype.Repository;

import com.hxf.common.GlobalConstant;
import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDao;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.preferential.PreferentialDao;
import com.hxf.dto.MealPreferentialDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.web.vo.PreferentialVo;

/**
 * DAO优惠实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/23]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("unchecked")
@Repository("preferentialDao")
public class PreferentialDaoImpl extends BaseDaoImpl implements PreferentialDao {

	@Override
	public ReturnData<MealPreferentialDto> findMealPreferential(MealPreferentialDto mealPreferentialDto, int targetPage, int pageSize) throws DaoLevelException {
		String sql = "";
		if(null != mealPreferentialDto){
			if (mealPreferentialDto != null && mealPreferentialDto.getRestaurantId() != null) {
				sql = " and restaurantId = " + mealPreferentialDto.getRestaurantId();
			}
			
			if(PublicUtil.isNotNullAndEmpty(mealPreferentialDto.getName())){
				sql = " and name like '%"+mealPreferentialDto.getName().trim()+"%' ";
			}
		}
		
		return super.findBy("from MealPreferentialDto where 1=1 " + sql , targetPage, pageSize);
		/*String hql = "SELECT m.id,begin_date,copies,cost_price,discount,end_date,explanation," +
			"images,meal_menu_id,meal_package,m.name,package_remark,preferential_price,preferential_type," +
			"release_status,restaurant_id,m.status AS STATUS,r.name FROM rk_meal_preferential AS m " +
			"LEFT JOIN rk_restaurant AS r ON m.restaurant_id  = r.id";
		List meal = this.exeSQLList(hql);
		List<MealPreferentialVo> mealDtoList =  new ArrayList<MealPreferentialVo>();
		MealPreferentialVo mealPrefer = null;
		
		if(null != meal || 0 != meal.size()){
			for(int i=0;i<meal.size();i++){
				Object[] obj = (Object[])meal.get(i);
				mealPrefer = new MealPreferentialVo();
				mealPrefer.setId(null==obj[0]?null:Integer.valueOf(obj[0].toString()));
				mealPrefer.setBeginDate(null==obj[1]?null:obj[1].toString());
				mealPrefer.setCopies(null==obj[2]?null:Integer.valueOf(obj[2].toString()));
				mealPrefer.setCostPrice(null==obj[3]?null:Double.valueOf(obj[3].toString()));
				mealPrefer.setDiscount(null==obj[4]?null:Double.valueOf(obj[4].toString()));//登录时间
				mealPrefer.setEndDate(null==obj[5]?null:obj[5].toString());
				mealPrefer.setExplanation(null==obj[6]?null:obj[6].toString());//真实名称
				mealPrefer.setImages(null==obj[7]?null:obj[7].toString());//状态
				mealPrefer.setMealMenuId(null==obj[8]?null:obj[8].toString());//地址
				mealPrefer.setMealPackage(null==obj[9]?null:obj[9].toString());//Email
				mealPrefer.setName(null==obj[10]?null:obj[10].toString());//身份证号码
				mealPrefer.setPackageRemark(null==obj[11]?null:obj[11].toString());//固定电话
				mealPrefer.setPreferentialPrice(null==obj[12]?null:Integer.valueOf(obj[12].toString()));//价格
				mealPrefer.setPreferentialType(null==obj[13]?null:Integer.valueOf(obj[13].toString()));//type
				mealPrefer.setReleaseStatus(null==obj[14]?null:Boolean.valueOf(obj[14].toString()));//密码
				mealPrefer.setRestaurantId(null==obj[15]?null:Integer.valueOf(obj[15].toString()));
				mealPrefer.setStatus(null==obj[16]?null:Integer.valueOf(obj[16].toString()));//性别
				mealPrefer.setRestaurentName(null==obj[17]?null:obj[17].toString());//用户类型
				
				mealDtoList.add(mealPrefer);
			}
			
			ReturnData<MealPreferentialDto> returnMeal = null;
			returnMeal.setResultlist(mealDtoList);
		}
		
		return null;*/
	}
	
	@Override
	public ReturnData<PreferentialVo> findMealPreferentialByRestaurant(RestaurantDto restaurantDto, MealPreferentialDto mealPreferentialDto, int targetPage, int pageSize) throws DaoLevelException { 
		String sql = ""; 
		if (restaurantDto != null) {
			//名称
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getName())){
				 sql +=" and name = '"+restaurantDto.getName()+"'";
			}
			
			//省份
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getProvince())){
				sql += " and province = '"+restaurantDto.getProvince().trim()+"' ";
			}
			
			//市
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getCity())){
				sql += " and city = '"+restaurantDto.getCity().trim()+"' ";
			}
				
			//区
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getDistrict())){
				sql += " and district like '"+restaurantDto.getDistrict().trim()+"'% ";
			}
			
			//街道
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getStreet())){
				sql += " and street like '"+restaurantDto.getDistrict().trim()+"'% ";
			}
			 
		}
		
		if (mealPreferentialDto != null) {
			if (PublicUtil.isNotNullAndEmpty(mealPreferentialDto.getName())) {
				sql += " and menuName like '"+mealPreferentialDto.getName().trim()+"'% ";
			}
			
			if (PublicUtil.isNotNullAndEmpty(mealPreferentialDto.getPackageRemark())) {
				sql += mealPreferentialDto.getPackageRemark();
			}
		}
		
		ReturnData rd = super.exeSQLList("select rmm.id,copies,cost_price,discount,begin_date,end_date,rmm.explanation,images,rmm.name,preferential_price,res.name from rk_restaurant as res,rk_meal_preferential rmm where res.id=rmm.restaurant_id" + sql, targetPage, pageSize);
		List list = rd.getResultlist();
		if (list != null && list.size() > 0) {
			List<PreferentialVo> mpDtoList = new ArrayList<PreferentialVo>();
			PreferentialVo preferentialDto = null;
			Object[] o = null;
			for (int i =0;i < list.size(); i++) {
				preferentialDto = new PreferentialVo();
				o = (Object[])list.get(i);
				preferentialDto.setId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				preferentialDto.setCopies(null==o[1]?null:Integer.parseInt(o[1].toString()));
				preferentialDto.setCostPrice(null==o[2]?null:Double.parseDouble(o[2].toString()));
				preferentialDto.setDiscount(null==o[3]?null:Double.parseDouble(o[3].toString()));
				preferentialDto.setBeginDate(null==o[4]?null:o[4].toString());
				preferentialDto.setEndDate(null==o[5] ?null:o[5].toString());
				preferentialDto.setExplanation(null==o[6]?null:o[6].toString());
				if (null != o[7] && !"".equals(o[7].toString())) {
					preferentialDto.setImageUrl(GlobalConstant.WEB_URL+GlobalConstant.PROJECT_API+o[7].toString());
				} else {
					preferentialDto.setImageUrl(" ");
				}
				
				preferentialDto.setName(null==o[8]?null:o[8].toString());
				preferentialDto.setPreferentialPrice(null==o[9]?null:Double.parseDouble(o[9].toString()));
				preferentialDto.setRestaurantName(null==o[10]?null:o[10].toString());
				mpDtoList.add(preferentialDto);
			}
			rd.setResultlist(mpDtoList);
		}
		return rd;
	}

	@Override
	public MealPreferentialDto findMealPreferentialByMealMenuId(Integer mealMenuId) throws DaoLevelException {
		List<MealPreferentialDto> mealPreferentialList = super.findBy("from MealPreferentialDto where mealMenuId = " + mealMenuId);
		if (mealPreferentialList != null) {
			return mealPreferentialList.get(0);
		} else {
			return new MealPreferentialDto();
		} 
	}

	@Override
	public List<MealPreferentialDto> findMealPreferential(MealPreferentialDto mealPreferentialDto) throws DaoLevelException {
		String sql = "";
		if (mealPreferentialDto != null) {
			if (mealPreferentialDto.getRestaurantId() != null) {
				sql = " and restaurantId = " + mealPreferentialDto.getRestaurantId();
			}
			
			if (mealPreferentialDto.getPreferentialType() != null) {
				sql = " and preferentialType = " + mealPreferentialDto.getPreferentialType();
			}
		}
		 
		return super.findBy("from MealPreferentialDto where 1=1" + sql);
	} 
}
